
tinymce.PluginManager.add('checkbox', function(editor, url){

    editor.ui.registry.addButton('checkbox', {
        icon: 'selected',
        tooltip: '插入复选框',
        onAction: ()=>{
            var id = Math.uuid().toLowerCase();
            editor.insertContent('<input type="checkbox" class="mce-checkbox" data-id="' + id +'" />')
        },
        onSetup: ()=>{
        }
    });

    editor.on('init', function () {
        const body = editor.contentDocument.body;
        if ($(body).hasClass("view_body")) {
            return;
        }
        body.addEventListener('input', function(e) {
            var $target = $(e.target);
            if ($target.is("input")) {
                if ($target.attr("checked")) {
                    $target.removeAttr("checked");
                    $target.addClass("checkbox-blank");
                } else {
                    $target.attr("checked", "checked");
                    $target.removeClass("checkbox-blank");
                }
                if (!$target.attr("data-group-id") && !$target.attr("data-id")) {
                    $target.attr("data-id", Math.uuid().toLowerCase());
                }
            }
        }, false);
    });

    return {
        getMetadata: function () {
          return  {
            name: "checkbox",//插件名称
            url: "http://exampleplugindocsurl.com", //作者网址
          };
        }
    };

})